package com.kanas.webak.organization.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.organization.service.EventService;
import com.kanas.webak.organization.view.Event;

@Controller
@RequestMapping("app/organization/event")
public class EventController extends ControllerSupport{
	private EventService eventService;

	public EventService getEventService() {
		return eventService;
	}
	
	@Resource(name="com.kanas.webak.organization.service.eventservice")
	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}
	
	@RequestMapping(value="/eventbymonth",method=RequestMethod.GET)
	public @ResponseBody List<Event> listByMonth(@RequestParam(value="orgId",required=false)String orgId,@RequestParam(value="start",required=false)String month) throws ParseException {
		Date viewDay = new Date(Long.parseLong(month)); 
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String viewDayStr = format.format(viewDay);
		String nowMonthStr = viewDayStr.substring(0,8)+"01";
		Date nowMonth = format.parse(nowMonthStr);
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(nowMonth);
		cal.set(java.util.Calendar.DAY_OF_MONTH, cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));
		Date nextMonth = cal.getTime();
		System.out.println(nextMonth);
		List<Event> response = eventService.findEventByOrgByMonth(orgId, nowMonth, nextMonth);
		return response;
	}
}
